<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;

Route::get('think', function () {
    return 'hello,ThinkPHP6!';
});

Route::get('hello/:name', 'index/hello');

// 登录接口（独立路由，仅CORS中间件）
Route::post('api/auth/login', 'AuthController/login')->middleware('cors');

// 处理所有API的OPTIONS预检请求
Route::options('api/:any', function() {
    return response('', 204);
})->middleware('cors')->pattern(['any' => '.*']);

// 需要登录验证的API路由
Route::group('api', function () {
    // 认证相关（只需要登录验证，不需要权限验证）
    Route::group(function () {
        Route::post('auth/logout', 'AuthController/logout');
        Route::get('auth/info', 'AuthController/info');
        Route::post('auth/change-password', 'AuthController/changePassword');
        Route::post('auth/update-profile', 'AuthController/updateProfile');
    })->middleware(['cors', 'auth']);
    
    // 需要权限验证的API路由
    Route::group(function () {
        // 管理员管理 - 具体路由必须在通用路由之前
        Route::get('admins/:id', 'AdminController/read');
        Route::put('admins/:id', 'AdminController/update');
        Route::delete('admins/:id', 'AdminController/delete');
        Route::patch('admins/:id/status', 'AdminController/changeStatus');
        Route::get('admins', 'AdminController/index');
        Route::post('admins', 'AdminController/save');
        Route::delete('admins', 'AdminController/batchDelete');
        
        // 角色权限管理（必须放在通用角色路由之前）
        Route::get('roles/:id/permissions', 'RoleController/getPermissions');
        Route::put('roles/:id/permissions', 'RoleController/updatePermissions');
        
        // 角色管理
        Route::get('roles', 'RoleController/index');
        Route::get('roles/all', 'RoleController/all');
        Route::get('roles/:id', 'RoleController/read');
        Route::post('roles', 'RoleController/save');
        Route::put('roles/:id', 'RoleController/update');
        Route::delete('roles/:id', 'RoleController/delete');
        Route::delete('roles', 'RoleController/batchDelete');
        Route::patch('roles/:id/status', 'RoleController/changeStatus');
        
        // 权限管理
        Route::get('permissions', 'PermissionController/index');
        Route::get('permissions/tree', 'PermissionController/tree');
        Route::get('permissions/:id', 'PermissionController/read');
        Route::post('permissions', 'PermissionController/save');
        Route::put('permissions/:id', 'PermissionController/update');
        Route::delete('permissions/:id', 'PermissionController/delete');
        Route::delete('permissions', 'PermissionController/batchDelete');
        Route::patch('permissions/:id/status', 'PermissionController/changeStatus');
        
        // 菜单管理
        Route::get('menus', 'MenuController/index');
        Route::get('menus/tree', 'MenuController/tree');
        Route::get('menus/router', 'MenuController/router');
        Route::get('menus/:id', 'MenuController/read');
        Route::post('menus', 'MenuController/save');
        Route::put('menus/:id', 'MenuController/update');
        Route::delete('menus/:id', 'MenuController/delete');
        Route::delete('menus', 'MenuController/batchDelete');
        Route::patch('menus/:id/status', 'MenuController/changeStatus');
    })->middleware(['cors', 'auth', 'permission']);
});
